package com.zdb.datastructures.stack;

public class ArrayStackDemo {

    public static void main(String[] args) {

        ArrayStack stack = new ArrayStack(5);
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.push(5);
        stack.print();

        stack.pop();
        stack.pop();
        stack.pop();
        stack.pop();
        stack.print();
        stack.pop();
        stack.print();
    }
}

class ArrayStack {

    private int maxeSize;
    private int[] arr;
    private  int top = -1;

    public ArrayStack(int maxeSize) {
        this.maxeSize = maxeSize;
        arr = new int[maxeSize];
    }

    public boolean isFull() {
        return top == maxeSize -1;
    }

    public void push(int i) {
        if(isFull()) {
            throw new RuntimeException("栈已经满了");
        }

        top++;
        arr[top] = i;
    }

    public boolean isEmpty() {
        return top == -1;
    }

    public int pop() {
        if(isEmpty()) {
            throw new RuntimeException("栈为空");
        }

        int v = arr[top];
        top--;
        return v;
    }

    public void print() {
        if(isEmpty())  {
            System.out.println("栈为空");
            return;
        }
        for(int i=top; i>-1; i--) {
            System.out.print(arr[i] + " -> ");
        }
        System.out.println();
    }

    

}
